package com.fintech.common.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import com.fintech.common.enums.DateFormatEnum;

/**
 * 日期时间工具类
 * @author wan.wei
 *
 */
public class DateTimeUtil {
	
	/**
	 * 根据出生日期 获取年龄
	 * @param birthDay
	 * @return
	 * @throws Exception
	 */
	public static int getAge(Date birthDay) throws Exception {
        Calendar cal = Calendar.getInstance();
 
        if (cal.before(birthDay)) {
            throw new IllegalArgumentException(
                "The birthDay is before Now.It's unbelievable!");
        }
        int yearNow = cal.get(Calendar.YEAR);
        int monthNow = cal.get(Calendar.MONTH);
        int dayOfMonthNow = cal.get(Calendar.DAY_OF_MONTH);
        cal.setTime(birthDay);
 
        int yearBirth = cal.get(Calendar.YEAR);
        int monthBirth = cal.get(Calendar.MONTH);
        int dayOfMonthBirth = cal.get(Calendar.DAY_OF_MONTH);
 
        int age = yearNow - yearBirth;
 
        if (monthNow <= monthBirth) {
            if (monthNow == monthBirth) {
                if (dayOfMonthNow < dayOfMonthBirth) age--;
            }else{
                age--;
            }
        }
        return age;
    }
	/**
	 * 根据制定格式，格式化时间
	 * @param dateFormat
	 * @return
	 */
	public static String formatDate(DateFormatEnum dateFormat,Date date){
		SimpleDateFormat sdf = new SimpleDateFormat(dateFormat.getFormat());
		return sdf.format(date);
	}
	/**
	 * 符合制定格式的时间字符串，转为时间
	 * @param dateFormat
	 * @param strDate
	 * @return
	 */
	public static Date formatStrDate(DateFormatEnum dateFormat,String strDate){
		SimpleDateFormat sdf = new SimpleDateFormat(dateFormat.getFormat());
		try {
			return sdf.parse(strDate);
		} catch (ParseException e) {
			e.printStackTrace();
			return null;
		}
	}
	/**
	 * 判断两个日期是否在同一天
	 * @param date1
	 * @param date2
	 * @return
	 */
	public static boolean isSameDate(Date date1, Date date2) {
	       Calendar cal1 = Calendar.getInstance();
	       cal1.setTime(date1);

	       Calendar cal2 = Calendar.getInstance();
	       cal2.setTime(date2);

	       boolean isSameYear = cal1.get(Calendar.YEAR) == cal2
	               .get(Calendar.YEAR);
	       boolean isSameMonth = isSameYear
	               && cal1.get(Calendar.MONTH) == cal2.get(Calendar.MONTH);
	       boolean isSameDate = isSameMonth
	               && cal1.get(Calendar.DAY_OF_MONTH) == cal2
	                       .get(Calendar.DAY_OF_MONTH);

	       return isSameDate;
	   }
	/**
	 * 以天数为单位进行实践比较
	 * @param date1
	 * @param compareStr
	 * @param date2
	 * @return
	 */
	public static Boolean compareDay(Date date1,String compareStr, Date date2){
		long dateTime1 = date1.getTime();
		long dateTime2 = date2.getTime();
		long day1=dateTime1/(1000*60*60*24);
		long day2=dateTime2/(1000*60*60*24);
		System.out.println("day1:"+day1+"##"+compareStr+"##day2:"+day2);
		if(compareStr.equals("<")){
			if(day1<day2){
				return true;
			}
		}else if(compareStr.equals("<=")){
			if(day1<=day2){
				return true;
			}
		}else if(compareStr.equals("==")){
			if(day1==day2){
				return true;
			}
		}else if(compareStr.equals(">=")){
			if(day1>=day2){
				return true;
			}
		}else if(compareStr.equals(">")){
			if(day1>day2){
				return true;
			}
		}else{
			return null;
		}
		return false;
	}
	/**
	 * 获得当月的第一天和最后一天
	 * @return
	 */
	public static Date[]  getCurrentMothStartAndEnd() {  
        // 获取当前年份、月份、日期  
        Calendar cale = null;  
        cale = Calendar.getInstance();  
        int year = cale.get(Calendar.YEAR);  
        int month = cale.get(Calendar.MONTH) + 1;  
        int day = cale.get(Calendar.DATE);  
        int hour = cale.get(Calendar.HOUR_OF_DAY);  
        int minute = cale.get(Calendar.MINUTE);  
        int second = cale.get(Calendar.SECOND);  
        int dow = cale.get(Calendar.DAY_OF_WEEK);  
        int dom = cale.get(Calendar.DAY_OF_MONTH);  
        int doy = cale.get(Calendar.DAY_OF_YEAR);  
  
        System.out.println("Current Date: " + cale.getTime());  
        System.out.println("Year: " + year);  
        System.out.println("Month: " + month);  
        System.out.println("Day: " + day);  
        System.out.println("Hour: " + hour);  
        System.out.println("Minute: " + minute);  
        System.out.println("Second: " + second);  
        System.out.println("Day of Week: " + dow);  
        System.out.println("Day of Month: " + dom);  
        System.out.println("Day of Year: " + doy);  
  
        // 获取当月第一天和最后一天  
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");  
        String firstday, lastday;  
        // 获取前月的第一天  
        cale = Calendar.getInstance();  
        cale.add(Calendar.MONTH, 0);  
        cale.set(Calendar.DAY_OF_MONTH, 1);  
        firstday = format.format(cale.getTime());  
        // 获取前月的最后一天  
        cale = Calendar.getInstance();  
        cale.add(Calendar.MONTH, 1);  
        cale.set(Calendar.DAY_OF_MONTH, 0);  
        lastday = format.format(cale.getTime());
        Date endDate = formatStrDate(DateFormatEnum.yyyy_MM_dd, lastday);
        return new Date[]{formatStrDate(DateFormatEnum.yyyy_MM_dd, firstday),lastSecondDate(endDate)};
    }
	
	/**
	 * 获取当天的第一秒的date
	 * @param date
	 * @return
	 */
	public static Date firstSecondDate(Date date){
		return DateTimeUtil.formatStrDate(DateFormatEnum.yyyy_MM_dd, DateTimeUtil.formatDate(DateFormatEnum.yyyy_MM_dd,date));
	}
	/**
	 * 获取当天的最后一秒的date
	 * @param date
	 * @return
	 */
	public static Date lastSecondDate(Date date){
		long time = new Date().getTime()+24*60*60*1000;
		Date endDate = firstSecondDate(new Date(time));
		time=endDate.getTime()-1000;
		return new Date(time);
	}
	
	/**
	 * 时间移动指定天数
	 * @param day 需要移动的天数
	 * @param date 需要移动的日期
	 * @return
	 */
	public static Date moveDay(int day,Date date){
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.set(Calendar.DATE, calendar.get(Calendar.DATE) + day);
		return calendar.getTime();
	}
    
	/**
	 * 返回两个年份的对比  多用于根据出生年份获取年龄
	 * @param oldYear
	 * @return
	 */
	public static Integer getAge(Integer oldYear){
		Calendar a = Calendar.getInstance();
		return a.get(Calendar.YEAR) - oldYear;
	}
	
	/**
	 * 天数向前移动
	 * @param day
	 * @param date
	 * @return yyyyMMdd
	 */
	public static String prevMoveDay(int day,Date date){
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.set(Calendar.DATE, calendar.get(Calendar.DATE) - day);
		SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMdd");//这个是你要转成后的时间的格式
		String sd = sdf.format(calendar.getTime());   // 时间戳转换成时间
		 return sd;
	}
	
	
	/**
	 * 增加几分钟或者减少几分钟  当前时间
	 * @param minute
	 * @return
	 */
	 public static Date getTimeByMinute(int minute) {
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.MINUTE, minute);
        return calendar.getTime();
	 }
	 
	 public static void main(String[] args) {
		 Date timeByMinute = getTimeByMinute(10);
		 System.out.println(timeByMinute);
		 Date timeByMinute2 = getTimeByMinute(-10);
		 System.out.println(timeByMinute2);
	}
	 
}
